/**
 * 收费方式管理初始化
 */
var CheckoutMethod = {
    id: "CheckoutMethodTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
CheckoutMethod.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '', field: 'id', visible: false, align: 'center', valign: 'middle', width: '50px'},
        {title: '编码', field: 'num', align: 'center', valign: 'middle', sortable: true},
        {title: '名称', field: 'checkoutMethod', align: 'center', valign: 'middle', sortable: true},
        {title: '备注', field: 'remarks', align: 'center', valign: 'middle', sortable: true},
        {title: '收费账单', field: '', align: 'center', valign: 'middle', sortable: true,
            formatter: function (value, row, index) {
                return "<div style='display: flex;align-items: center;'><span style='flex: 1;' class='switch_on'></span><span style='flex: 3;padding-left: 15px;text-align: left;border-left: 1px solid #DDDDDD'>" +
                    "<label style='display: block'><input type='checkbox' name='cashFlow' value='现金流'><span style='margin-left: 10px;'>计入现金流</span></label>" +
                    "<label style='display: block'><input type='checkbox' name='businessIncome' value='营业收入'><span style='margin-left: 10px;'>计入营业收入</span></label>" +
                    "<label style='display: block'><input type='checkbox' name='achievements' value='绩效'><span style='margin-left: 10px;'>计入绩效</span></label></span></div>"
            }
        },
        {title: '储值卡充值', field: '', align: 'center', valign: 'middle', sortable: true,
            formatter: function (value, row, index) {
                return "<div style='display: flex;align-items: center;'><span style='flex: 1' class='switch_on'></span><span style='flex: 3;padding-left: 15px;text-align: left;border-left: 1px solid #DDDDDD'>" +
                    "<label style='display: block'><input type='checkbox' name='cashFlow' value='现金流'><span style='margin-left: 10px;'>计入现金流</span></label>" +
                    "<label style='display: block'><input type='checkbox' name='businessIncome' value='营业收入'><span style='margin-left: 10px;'>计入营业收入</span></label></span></div>";
            }
        },
        {title: '卡券发售', field: '', align: 'center', valign: 'middle', sortable: true,
            formatter: function (value, row, index) {
                return "<div style='display: flex;align-items: center;'><span style='flex: 1' class='switch_on'></span><span style='flex: 3;padding-left: 15px;text-align: left;border-left: 1px solid #DDDDDD'>" +
                    "<label style='display: block'><input type='checkbox' name='cashFlow' value='现金流'><span style='margin-left: 10px;'>计入现金流</span></label>" +
                    "<label style='display: block'><input type='checkbox' name='businessIncome' value='营业收入'><span style='margin-left: 10px;'>计入营业收入</span></label></span></div>";
            }
        }
    ];
};

/**
 * 检查是否选中
 */
CheckoutMethod.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        CheckoutMethod.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加收费方式
 */
CheckoutMethod.openAddCheckoutMethod = function () {
    var index = layer.open({
        type: 2,
        title: '添加收费方式',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/checkoutMethod/checkoutMethod_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看收费方式详情
 */
CheckoutMethod.openCheckoutMethodDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '收费方式详情',
            area: ['90%', '90%'], //宽高
            fix: true, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/checkoutMethod/checkoutMethod_update/' + CheckoutMethod.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除收费方式
 */
CheckoutMethod.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/checkoutMethod/delete/" + this.seItem.id, function (data) {
            Feng.success("删除成功!");
            CheckoutMethod.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.start();
    }
};


/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
CheckoutMethod.formParams = function () {
    var queryData = {};
    queryData['checkoutMethod'] = $("#checkoutMethod").val();
    return queryData;
}

CheckoutMethod.resetSearch = function () {
    $("#checkoutMethod").val("");
    CheckoutMethod.search();
};

/**
 * 查询列表
 */
CheckoutMethod.search = function () {
    CheckoutMethod.table.refresh({query: CheckoutMethod.formParams()});
};

$(function () {
    var defaultColunms = CheckoutMethod.initColumn();
    var table = new BSTable(CheckoutMethod.id, "/checkoutMethod/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(CheckoutMethod.formParams());
    CheckoutMethod.table = table.init();
    CheckoutMethod.table = table;

    $(document).on('click','.switch_on',function () {
        if($(this).hasClass('switch_off')){
            $(this).removeClass('switch_off')
        }else {
            $(this).addClass('switch_off')
        }
    })
});
